package com.rc.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.rc.activity.R;
import com.rc.activity.ReceiptBoxDetailEditItem;
import com.rc.activity.dao.tbl_CategoryDao;
import com.rc.activity.dao.tbl_Item;
import com.rc.activity.dao.tbl_Trip;
import com.rc.activity.provider.Contract;
import com.rc.db.DatabaseSupport;
import com.rc.util.Util;

public class ReceiptDetailAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<tbl_Item> items;
	public boolean isShowCheck = false;
	private CheckBox cbMulti;

	public ReceiptDetailAdapter(Context c, ArrayList<tbl_Item> item) {
		mContext = c;
		this.items = item;
	}

	public int getCount() {
		return items.size();
	}

	public Object getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new view for each item referenced by the Adapter
	@SuppressLint("ResourceAsColor")
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		TextView txtMoney, txtNameReceipt, txtReceiptType;

		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			LayoutInflater vi = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.receiptdetail_itemlist, null);

		}
		cbMulti = (CheckBox) v.findViewById(R.id.cbMulti);
		txtMoney = (TextView) v.findViewById(R.id.txtMoney);
		txtReceiptType = (TextView) v.findViewById(R.id.txtReceiptType);
		txtNameReceipt = (TextView) v.findViewById(R.id.txtNameReceipt);

		if (!isShowCheck) {
			cbMulti.setVisibility(View.GONE);
		} else {
			cbMulti.setVisibility(View.VISIBLE);
		}

		txtMoney.setText("$"
				+ Util.formatStringFloat(Util.convertFloat(items.get(position)
						.getAmount())));
		txtNameReceipt.setText(items.get(position).getName());
		long mCategoryId;
		String mCategoryText = "";
		mCategoryId = items.get(position).getCategoryID();
		if (mCategoryId > 0) {
			Cursor cursor = mContext.getContentResolver().query(
					Contract.CONTENT_URI_CATEGORY,
					null,
					tbl_CategoryDao.Properties.CategoryID.columnName + " = '"
							+ mCategoryId + "'", null, null);
			tbl_Trip objTrip = new tbl_Trip();
			objTrip = DatabaseSupport.getTripByID(items.get(position)
					.getTripID(), mContext);
			if (cursor.moveToNext()) {
				if (objTrip != null) {
					mCategoryText = objTrip.getReference()
							+ " > "
							+ cursor.getString(cursor
									.getColumnIndex(tbl_CategoryDao.Properties.Name.columnName));
				}

			}
			cursor.close();
		}
		txtReceiptType.setText(mCategoryText);
		cbMulti.setChecked(items.get(position).isCheck());
		v.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isShowCheck) {
					cbMulti = null;
					cbMulti = ((CheckBox) v.findViewById(R.id.cbMulti));
					if (cbMulti.isChecked()) {
						cbMulti.setChecked(false);
						items.get(position).setCheck(false);
					} else {
						cbMulti.setChecked(true);
						items.get(position).setCheck(true);
					}
				} else {
					ReceiptBoxDetailEditItem.callIntent(mContext,
							items.get(position));
				}
			}
		});
		return v;
	}

	public void notiB(ArrayList<tbl_Item> arrReceiptBoxObj) {
		this.items = arrReceiptBoxObj;
		notifyDataSetChanged();
	}

}